/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain.controller.file;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Stack;

/**
 *
 * @author Novo usuário
 */
public class ControllerFileHelper {

    public final static String WORK_DIR = "/data/";
    private final Hashtable descFilesCache = new Hashtable(0);
    private final int lineSeparator = '\n';
    private static ControllerFileHelper intance = new ControllerFileHelper();

    private ControllerFileHelper() {
    }

    public static ControllerFileHelper getIntance() {
        return intance;
    }

    public Stack getDescFileNames(String descFileName) {

        Stack file = (Stack) descFilesCache.get(descFileName);
        if (file == null) {
            file = new Stack();
            InputStream iis = getClass().getResourceAsStream(WORK_DIR + descFileName);
            StringBuffer sb = new StringBuffer();
            int cha;
            try {
                while ((cha = iis.read()) != -1) {
                    if (cha == lineSeparator) {
                        file.addElement(sb.toString());
                        sb = new StringBuffer();
                        continue;
                    }
                    sb.append((char) cha);
                }
                descFilesCache.put(descFileName, file);
                iis = null;
                sb = null;
                System.gc();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return (Stack) descFilesCache.get(descFileName);
    }

    public String readFile(String fname){

        String stream = "";
        int cha;
        try {
            InputStream is = getClass().getResourceAsStream(WORK_DIR + fname);
            while ((cha = is.read()) != -1) {
                stream += String.valueOf((char) cha);
            }
        }catch(IOException ex){
            ex.printStackTrace();
        }
        return stream;
    }

}
